Skip to content

gh-143231: Do not swallow not matched warnings in assertWarns*()#149229

Merged
serhiy-storchaka merged 7 commits intopython:mainfrom
serhiy-storchaka:unittest-assertWarns-no-swallow
May 3, 2026
Merged

gh-143231: Do not swallow not matched warnings in assertWarns*()#149229
serhiy-storchaka merged 7 commits intopython:mainfrom
serhiy-storchaka:unittest-assertWarns-no-swallow

Conversation

@serhiy-storchaka
Copy link
Copy Markdown
Member

@serhiy-storchaka serhiy-storchaka commented May 1, 2026

unittest.TestCase methods assertWarns() and assertWarnsRegex() no longer swallow warnings that do not match the specified category or regex. Nested context managers are now supported.

unittest.TestCase methods assertWarns() and assertWarnsRegex() no longer
swallow warnings that do not match the specified category or regex.
Nested context managers are now supported.
@read-the-docs-community
Copy link
Copy Markdown

read-the-docs-community Bot commented May 1, 2026

Documentation build overview

📚 cpython-previews | 🛠️ Build #32514225 | 📁 Comparing 6b0080a against main (f2c7c0d)

  🔍 Preview build  

41 files changed · ± 41 modified

± Modified

Copy link
Copy Markdown
Member

@bitdancer bitdancer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Has the behavior of record=True changed at all?

Comment on lines +409 to +410
self.assertWarnsRegex(UserWarning, 'foo', self.assertMessagesCM,
'assertWarnsRegex', (UserWarning, 'regex'),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I read this I thought at first that you were calling assertWarnsRegex directly instead of calling assertMessagesCM. Even after understanding what you are actually doing it is hard to understand exactly what this is testing. If I understand the difference correctly (now it emits the warning where before it didn't, but that's not what the test is testing), then I think it would be easier to understand if you wrote it like this:

Suggested change
self.assertWarnsRegex(UserWarning, 'foo', self.assertMessagesCM,
'assertWarnsRegex', (UserWarning, 'regex'),
with self.assertWarnsRegex(UserWarning, 'foo'):
self.assertMessagesCM('assertWarnsRegex', (UserWarning, 'regex'),

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is for the callable interface of assertWarnsRegex(). The next test is for the context manager interface. I think it is better to not mix them in these tests.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I understand that. But that part of the test is in the inner call to assertMessagesCM, which is the thing calling assertWarnsRexex multiple times using the callable interface. The fact that a warning that is not caught is also emitted is a consequence of the nature of the test, but that's a byproduct, and by using the assertWarns context manager we indicate that we are expecting that byproduct.

If you want to leave it the way you have it I won't block on it ;)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I missed this with other tests in test_case where I use nested callables. Here, I used the callable form only because it was the minimal change. But a context manager looks more readable.

Comment thread Lib/test/test_unittest/test_case.py
Comment thread Lib/test/test_unittest/test_case.py
Comment thread Lib/unittest/case.py Outdated
exc_name = str(self.expected)
first_matching = None
matched = False
not_matching_warnings = []
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standard English would make this variable name non_matching_warnings.

@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 1, 2026

When you're done making the requested changes, leave the comment: I have made the requested changes; please review again.

Copy link
Copy Markdown
Member

@bitdancer bitdancer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

…ing by module name.

Add the module attribute to warnings.WarningMessage.
@serhiy-storchaka
Copy link
Copy Markdown
Member Author

I fixed most of the remaining issues with minimal changes in the warnings module. User-visible changes are that warning filtering with actions "default" and "module" now works as expected, and filtering by module name now works.

It required more complex changes in unittest. I am planning to move them to the warnings module later, but this requires serious rewriting of the C code.

@serhiy-storchaka
Copy link
Copy Markdown
Member Author

Changes to the warnings module were merged in a separate PR #149298, with additional tests.

@serhiy-storchaka serhiy-storchaka enabled auto-merge (squash) May 3, 2026 10:17
@serhiy-storchaka serhiy-storchaka merged commit a3435d5 into python:main May 3, 2026
51 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants